const express = require("express");
const router = express.Router();
const jwt = require("jsonwebtoken");
const { secretKey, expiresIn } = require("../../config/jwtConfig");
const redisClient = require('../../config/redis')
const redis = require('redis')
const {
  queryLogin,
  queryUserInfo,
  brandAddSql,
  brandDelSql,
  brandListSql,
  brandCountSql,
  userGroupAddSql,
  userGroupUpdateSql,
  userGroupListSql,
  userGroupCountSql,
  userGroupDelSql,
  userGroupAllSql,
  serviceAddSql,
  serviceUpdateSql,
  serviceListSql,
  serviceCountSql,
  serviceDelSql,
  serviceAllSql,
  userAddSql,
  userUpdateSql,
  userListSql,
  userCountSql,
  userDelSql,
} = require("../../connection/platform");
// 登录
router.post("/login", async function (req, res, next) {
  const data = req.body;
  const params = [data.username, data.password];
  try {
    await queryLogin(params);
    const token = jwt.sign(
      {
        account: data.username,
      },
      secretKey,
      { expiresIn }
    );
    res.status(200).json({
      msg: "登录成功",
      data: {
        token,
      },
    });
  } catch (e) {
    console.log(e);
    res.status(200).json({ msg: "登录失败，账号密码不正确" });
  }
});
// 获取用户信息
router.get("/getUserInfo", async function (req, res, next) {
  try {
    const { account } = req.user;
    const data = await queryUserInfo([account]);
    const { avatar, nickname, phone } = data[0];
    res.status(200).json({
      msg: "成功",
      data: {
        account,
        avatar,
        nickname,
        phone,
        roles: ['*']
      },
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 退出登录
router.post("/logout", async function(req, res, next) {
  try{
    await redisClient.connect()
    const token = req.headers.authorization;
    await redisClient.sAdd('logoutTokens', token, redis.print)
    await redisClient.quit()
    res.status(200).json({
      msg: '成功'
    })
  }catch(e){
    console.log(e)
  }
  return;
})
/**
 * 品牌管理
 */
// 新增品牌
router.post("/brandAdd", async function (req, res, next) {
  try {
    const { name, logo, user_id, package_id, status } = req.body;
    await brandAddSql([name, logo, user_id, package_id, status]);
    res.status(200).json({
      msg: "成功",
      data: {},
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 删除品牌
router.post("/brandDel", async function (req, res, next) {
  try {
    const { id } = req.body;
    await brandDelSql([id]);
    res.status(200).json({
      msg: "成功",
      data: {},
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 查询品牌列表
router.get("/bransList", async function (req, res, next) {
  try {
    const { limit, page } = req.query;
    const startIndex = (page - 1) * limit; // 开始查询的索引号
    const data = await brandListSql([startIndex, page]);
    const countData = await brandCountSql();
    const total = countData[0].count;
    res.status(200).json({
      msg: "成功",
      data: data,
      total: total,
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
/**
 * 用户组管理
 */
// 新增用户组
router.post("/userGroupAdd", async (req, res, next) => {
  try {
    const { sort, name, service_id, status } = req.body;
    await userGroupAddSql([sort, name, service_id, status]);
    res.status(200).json({
      msg: "成功",
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 修改用户组
router.post("/userGroupUpdate", async (req, res, next) => {
  try {
    const { sort, name, service_id, status, id } = req.body;
    await userGroupUpdateSql([sort, name, service_id, status, id]);
    res.status(200).json({
      msg: "成功",
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 查询用户组(分页)
router.get("/userGroupList", async (req, res, next) => {
  try {
    const { page, limit } = req.query;
    console.log(page, limit)
    const startIndex = (page - 1) * limit; // 开始查询的索引号
    const data = await userGroupListSql([startIndex, Number(limit)]);
    const countData = await userGroupCountSql();
    const total = countData[0].count;
    res.status(200).json({
      msg: "成功",
      data: data,
      total: total,
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 删除用户组
router.post("/userGroupDel", async (req, res, next) => {
  try {
    const { ids } = req.body;
    await userGroupDelSql(ids);
    res.status(200).json({
      msg: "成功",
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 查询全部用户组
router.get('/userGroupAll',async(req, res, next) => {
  try{
    const data = await userGroupAllSql()
    res.status(200).json({
      msg: '成功',
      data
    })
  }catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
})
/**
 * 客服管理
 */
// 新增客服
router.post("/serviceAdd", async (req, res, next) => {
  try {
    const { sort, name, phone, wx_code, status } = req.body;
    await serviceAddSql([sort, name, phone, wx_code, status]);
    res.status(200).json({
      msg: "成功",
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 修改用户客服
router.post("/serviceUpdate", async (req, res, next) => {
  try {
    const { sort, name, phone, wx_code, status, id } = req.body;
    await serviceUpdateSql([sort, name, phone, wx_code, status, id]);
    res.status(200).json({
      msg: "成功",
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 查询用户客服(分页)
router.get("/serviceList", async (req, res, next) => {
  try {
    const { page, limit } = req.query;
    const startIndex = (page - 1) * limit; // 开始查询的索引号
    const data = await serviceListSql([startIndex, Number(limit)]);
    const countData = await serviceCountSql();
    const total = countData[0].count;
    res.status(200).json({
      msg: "成功",
      data: data,
      total: total,
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 删除用户客服
router.post("/serviceDel", async (req, res, next) => {
  try {
    const { ids } = req.body;
    await serviceDelSql(ids);
    res.status(200).json({
      msg: "成功",
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 查询全部客服
router.get("/serviceAll", async (req, res, next) => {
  try {
    const data = await serviceAllSql([]);
    console.log(data)
    res.status(200).json({
      msg: "成功",
      data,
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
/**
 * 用户管理
 */
// 新增用户
router.post("/userAdd", async (req, res, next) => {
  try {
    const { phone, name, password, group_id, store_num, status } = req.body;
    await userAddSql([phone, name, password, group_id, store_num]);
    res.status(200).json({
      msg: "成功",
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 修改用户
router.post("/userUpdate", async (req, res, next) => {
  try {
    const { phone, name, group_id, store_num, status, id } = req.body;
    await userUpdateSql([ phone, name, group_id, store_num, status, id]);
    res.status(200).json({
      msg: "成功",
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 查询用户(分页)
router.get("/userList", async (req, res, next) => {
  try {
    const { page, limit } = req.query;
    const startIndex = (page - 1) * limit; // 开始查询的索引号
    const data = await userListSql([startIndex, limit]);
    const countData = await userCountSql();
    const total = countData[0].count;
    res.status(200).json({
      msg: "成功",
      data: data,
      total: total,
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
// 删除用户
router.post("/userDel", async (req, res, next) => {
  try {
    const { id } = res.body;
    await userDelSql(id);
    res.status(200).json({
      msg: "成功",
    });
  } catch (e) {
    res.status(500).json({ msg: "错误: " + e });
  }
});
module.exports = router;
